package com.atguiguNIO.channel;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelDemo01 {

    /**
     *
     * fileChannel 读数据到 buffer 中
     *
     * */
    public static void readTest () {


        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile("test.txt", "rw");
            //创建fileChannel
            FileChannel channel = randomAccessFile.getChannel();

            ByteBuffer buf  = ByteBuffer.allocate(1024);

            int  byteRead = channel.read(buf);

            while (byteRead != -1 ) {

                System.out.println("read   ==>  " + byteRead  );
                buf.flip();  //飞利浦


                while (buf.hasRemaining()) {

                    System.out.println( (char) buf.get());
                }

                buf.clear();
                byteRead  = channel.read(buf);


            }

            randomAccessFile.close();
            System.out.println("finish!");



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public static void write( ) {



        //获取文件通道；

        try {
            FileChannel fileChannel = new FileOutputStream(new File("test.txt")).getChannel();

            RandomAccessFile randomAccessFile = new RandomAccessFile("test.txt", "rw");
            fileChannel = randomAccessFile.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(100) ;   //如果你put 进去的内容超过了 allocate( )  上限,  就会报错;
            buffer.put("hello      world" .getBytes());

            buffer.flip();
            fileChannel.write(buffer);
            fileChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * 用fileChannel进行文件复制
     */
    public  static  void fileCopyWithFileChannel() {


        File oriFile  = new File("test.txt") ;
        File targetFile  = new File("test_copy.txt");
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        FileChannel fileChannelInput = null;
        FileChannel fileChannelOutput = null;
        try {

            fileInputStream = new FileInputStream(oriFile);
            fileOutputStream = new FileOutputStream(targetFile);
            //得到fileInputStream的文件通道
            fileChannelInput = fileInputStream.getChannel();
            //得到fileOutputStream的文件通道
            fileChannelOutput = fileOutputStream.getChannel();
            //将fileChannelInput通道的数据，写入到fileChannelOutput通道
            fileChannelInput.transferTo(0, fileChannelInput.size(), fileChannelOutput);   //  还有一个方法 transferFrom
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null)
                    fileInputStream.close();
                if (fileChannelInput != null)
                    fileChannelInput.close();
                if (fileOutputStream != null)
                    fileOutputStream.close();
                if (fileChannelOutput != null)
                    fileChannelOutput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {


        fileCopyWithFileChannel();
    }
}
